package com.example.com.daervideo.activitys.users;

import android.content.Intent;

import android.database.Observable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.com.daervideo.BR;
import com.example.com.daervideo.R;
import com.example.com.daervideo.activitys.BaseListActivity;
import com.example.com.daervideo.databinding.ActivityInvitFriendBinding;
import com.example.com.daervideo.entity.UserData;
import com.example.com.daervideo.initerface.ApiService;
import com.example.com.daervideo.utils.RetrofitClient;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Created by i7-1 on 2018/12/7.
 */

public class InvitFriendActivity extends BaseListActivity<ActivityInvitFriendBinding, BaseViewModel> {

    private ApiService apiService;

    @Override
    public int initContentView() {
        return R.layout.activity_invit_friend;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected TwinklingRefreshLayout getRefreshLayout() {
        return getBinding().iInclude.refreshLayout;
    }

    @Override
    public BaseViewModel initViewModel() {
        return new BaseViewModel(this);
    }

    @Override
    public void initData() {
        super.initData();
        apiService = RetrofitClient.getInstance().create(ApiService.class);
        RecyclerView recyclerView = getBinding().iInclude.recyclerview;
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new FAdapter());
        findViewById(R.id.to_invit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(InvitFriendActivity.this, InviteCodeActivity.class));
            }
        });
    }


    private class FAdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(InvitFriendActivity.this).inflate(R.layout.item_attende_m, parent, false);
            return new FHolder(inflate);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 0;
        }
    }

    private class FHolder extends RecyclerView.ViewHolder {
        public FHolder(View inflate) {
            super(inflate);
        }
    }

    @Override
    protected void getNetData(int page, int pageSize) {

        apiService.getMyInviteFriends(page, pageSize).compose(RxUtils.bindToLifecycle(this)) //请求与View周期同步
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {

                    }
                })
                .subscribe(new Consumer<BaseResponse<UserData>>() {
                    @Override
                    public void accept(BaseResponse<UserData> response) throws Exception {
                        if (response.getCode() == 0) {

                        } else {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }, new Consumer<ResponseThrowable>() {
                    @Override
                    public void accept(ResponseThrowable throwable) throws Exception {
                        ToastUtils.showShort(throwable.message);
                        throwable.printStackTrace();
                    }
                });

    }
}
